/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.controller;
import edu.upc.clase.demo.entity.Credential;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.Sabio;
import edu.upc.clase.demo.service.SabioService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;


/**
 *
 * @author PAPA
 */
@Controller("sabioController")
public class SabioController {
    
    @Autowired
    private SabioService usuarioService;
    private static Logger log = LoggerFactory.getLogger(UsuarioController.class);

    @RequestMapping("/sabio/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("sabio/index");
        List<Sabio> sabio = usuarioService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("sabio", sabio);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/sabio/new", method = RequestMethod.GET)
    public ModelAndView newUsuario() {
        ModelAndView mav = new ModelAndView("sabio/new");
        Sabio usuario = new Sabio();
        mav.getModelMap().put("sabio", usuario);
        return mav;
    }

    @RequestMapping(value = "/sabio/new", method = RequestMethod.POST)
    public String createUsuario(@ModelAttribute("sabio")Sabio sabio, SessionStatus status) {    
        usuarioService.insertar(sabio);
        status.setComplete();

        return "redirect:/pages/sabio/index";
        
    }
    
    @RequestMapping(value = "/sabio/edit", method = RequestMethod.GET)
    public ModelAndView editUsuario(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("sabio/edit");
        Sabio sabio = usuarioService.buscar(id);
        mav.getModelMap().put("sabio", sabio);
        return mav;        
    }
    
    @RequestMapping(value="/sabio/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("sabio") Sabio sabio, SessionStatus status) {
        usuarioService.actualizar(sabio);
        status.setComplete();
        return "redirect:/pages/sabio/index";
    }
    
    @RequestMapping("/sabio/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/sabio/index");
        Sabio sabio = usuarioService.buscar(id);
        usuarioService.eliminar(sabio);
        return mav;
    }
    
    @RequestMapping("/sabio/login")
    public ModelAndView login() {
        ModelAndView mav = new ModelAndView("usuarios/login");
        Credential credential = new Credential();
        mav.getModelMap().put("credential", credential);
        return mav;
    }    
    
    @RequestMapping(value = "/sabio/autenticar", method=RequestMethod.POST)
    public String autenticar(@ModelAttribute("credential") Credential credential,HttpServletRequest request) {
        if (usuarioService.autenticar(credential.getCorreo(), credential.getPassword())) {
            request.getSession().setAttribute("credential",credential);
            return "redirect:/pages/sabio/index";
        }
        return "redirect:/pages/sabio/login";
    }
    
    @RequestMapping("/sabio/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/pages/sabio/login";
    }        
    
    
    @RequestMapping(value = "/sabio/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("sabio/resultadoBusqueda");
        log.info("resultado = " + usuarioService.buscarPorNombre(criterio.getNombre()).size());
        mav.getModel().put("resultado",usuarioService.buscarPorNombre(criterio.getNombre()));
        return mav;
    }
    
}
